package Bai11;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;

public class Test {
	public static void save(SinhVien[] svs) throws IOException {
		RandomAccessFile raf = new RandomAccessFile(
				new File("SinhVien456.txt"), "rw");
		// chua mot byte ghi tong so sinh vien (8*n sinh vien) bytes ghi mssv
		int tongSoSV = svs.length;
		raf.write(tongSoSV);
		for (SinhVien sv : svs) {
			byte[] bId = sv.getId().getBytes();
			raf.write(bId);
		}
		/**
		 * Data: Ma so sinh vien chua 8byte MSSV + 30byte Ten + 6byte LOP +
		 * 10byte NgaySinh + 3byte Diem Tong cong: 57byte cho ma so sinh vien
		 */
		for (SinhVien sv : svs) {
			byte[] bId = sv.getId().getBytes();
			byte[] bten = new byte[30];
			byte[] tempTen = sv.getTen().getBytes();
			for (int i = 0; i < tempTen.length; i++) {
				bten[i] = tempTen[i];

			}
			byte[] blop = sv.getLop().getBytes();
			byte[] bNgaySinh = sv.getNgaySinh().getBytes();
			byte[] bdiem = String.valueOf(sv.getDiem()).getBytes();

			raf.write(bId);
			raf.write(bten);
			raf.write(blop);
			raf.write(bNgaySinh);
			raf.write(bdiem);

		}
		raf.close();

	}

	public static void load() throws IOException {
		RandomAccessFile raf = new RandomAccessFile(
				new File("SinhVien456.txt"), "rw");
		int tongSoSV = raf.read();
		System.out.println("Tong so sinh vien : " + tongSoSV);
		for (int i = 0; i < tongSoSV; i++) {
			byte[] mssv = new byte[8];
			raf.read(mssv);
            String MSSV = new String(mssv);
            System.out.println(MSSV);
		}
		System.out.println("--data--");
		 for (int i = 0; i < tongSoSV; i++) {
	            readSinhVien(raf);
	        }
	        raf.close();
	    }

		
	private static void readSinhVien(RandomAccessFile raf) throws IOException {
		 byte[] bMssv = new byte[8];
	        raf.read(bMssv);
	        String mssv = new String(bMssv);
	        //Ten
	        byte[] tempBTen = new byte[30];
	        raf.read(tempBTen);
	        int sizeTen = 0;
	        for (int t = 0; t < tempBTen.length; t++) {
	            byte b = tempBTen[t];
	            if (b == 0) {
	                break;
	            }
	            sizeTen++;
	        }
	        byte[] bTen = new byte[sizeTen];
	        for (int t = 0; t < bTen.length; t++) {
	            bTen[t] = tempBTen[t];
	        }
	        String ten = new String(bTen);
	        //Lop
	        byte[] bLop = new byte[6];
	        raf.read(bLop);
	        String lop = new String(bLop);
	        //Ngay sinh
	        byte[] bNgaySinh = new byte[10];
	        raf.read(bNgaySinh);
	        String ngaySinh = new String(bNgaySinh);
	        //Diem
	        byte[] bDiem = new byte[3];
	        raf.read(bDiem);
	        String diem = new String(bDiem);
	        SinhVien sinhVien = new SinhVien(mssv, ten, lop, ngaySinh, Double.valueOf(diem));
	        System.out.println(sinhVien);
	    }
	 public static void tim(String mssv) throws IOException {
	        RandomAccessFile raf = new RandomAccessFile(new File("SinhVien456.txt"), "rw");
	        int tongSoSV = raf.read();
	        int index = -1;
	        for (int i = 0; i < tongSoSV; i++) {
	            byte[] bMssv = new byte[8];
	            raf.read(bMssv);
	            String MSSV = new String(bMssv);
	            if (mssv.equalsIgnoreCase(MSSV)) {
	                index = i;
	            }
	        }
	        if (index > -1) {
	            raf.skipBytes(index * 57);
	            System.out.print("Tim thay: ");
	            readSinhVien(raf);
	        } else {
	            System.out.println("Khong tim thay MSSV: " + mssv);
	        }
	    }




		
	


	

	public static void main(String[] args) throws IOException {
		SinhVien[] svs = new SinhVien[10];
        svs[0] = new SinhVien("08130041", "Nguyen Duy Khanh", "DH08DT", "17/07/1990", 8.0);
        svs[1] = new SinhVien("08130042", "Phan Tan Thanh", "DH08DT", "18/08/1990", 7.0);
        svs[2] = new SinhVien("08130043", "Nguyen Khoa", "DH08DT", "19/09/1990", 6.0);
        svs[3] = new SinhVien("08130044", "Le van A", "DH08DT", "19/09/1990", 6.0);
        svs[4] = new SinhVien("08130045", "Pham van B", "DH08DT", "19/09/1990", 6.0);
        svs[5] = new SinhVien("08130046", "Bui thi C", "DH08DT", "19/09/1990", 6.0);
        svs[6] = new SinhVien("08130047", "Lu phung D", "DH08DT", "19/09/1990", 6.0);
        svs[7] = new SinhVien("08130048", "Vo thi E", "DH08DT", "19/09/1990", 6.0);
        svs[8] = new SinhVien("08130049", "Tran Van F", "DH08DT", "19/09/1990", 6.0);
        svs[9] = new SinhVien("08130050", "Tong Thanh Trung", "DH08DT", "19/09/1990", 6.0);
        System.out.println("SAVE....");
        save(svs);
        System.out.println("SAVE SUCCESSFUL!");
        System.out.println("LOAD....");
        load();
        System.out.println("LOAD SUCCESSFUL!");
        while (true) {
            System.out.println("Nhap MSSV can tim : ");
            Scanner scanner = new Scanner(System.in);
            String mssv = scanner.nextLine();
            System.out.println("Dang Tim " + mssv + "...");
            tim(mssv);


	}

	}
}
